package org.acme.hibernate.orm.panache;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import io.quarkus.rest.client.reactive.QuarkusRestClientBuilder;
import io.vertx.core.json.JsonObject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;

import java.net.URI;

@Path("petty")
public class PettyResource {


    private final PettyService pettyService;

    public PettyResource(NamingService namingService) {
        try {
            Instance instance = namingService.selectOneHealthyInstance("finance-backend");
            pettyService = QuarkusRestClientBuilder.newBuilder()
                    .baseUri(URI.create("http://" + instance.getIp() + ":" + instance.getPort()))
                    .build(PettyService.class);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }

    }


    @POST
    public Object get(JsonObject param) {
        return pettyService.get(param);
    }

    @GET
    @Path("/{id}")
    public Object getAcceptancePhase(@PathParam("id") String id) {
        return pettyService.getAcceptancePhase(id);
    }

}
